package com.gitee.sidihuo.utils.util;


import com.gitee.sidihuo.utils.UtilsJarCompiler;
import com.gitee.sidihuo.utils.exception.AssertException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class AssertUtils {

    static {
        log.info("UtilsJarCompiler version = {}", UtilsJarCompiler.compileTime);
    }

    /**
     * 校验Rest接口入参的合法性
     *
     * @param legal      合法与否
     * @param script     提示话术
     * @param developMsg 开发排查问题提示信息
     */
    public static void assertRestFormLegal(boolean legal, String script, String developMsg) {
        if (legal) {
            return;
        }
        AssertException assertException = new AssertException(script, developMsg);
        log.warn("校验接口入参 用户提示信息 = {}  开发提示信息 = {}", script, developMsg);
        throw assertException;
    }

    /**
     * 校验上下文参数的合法性
     *
     * @param legal      合法与否
     * @param script     提示话术
     * @param developMsg 开发排查问题提示信息
     */
    public static void assertContextParaLegal(boolean legal, String script, String developMsg) {
        if (legal) {
            return;
        }
        AssertException assertException = new AssertException(script, developMsg);
        log.warn("校验上下文参数 用户提示信息 = {}  开发提示信息 = {}", script, developMsg);
        throw assertException;
    }


    public static void assertNotBlank(String str, String script, String developMsg) {
        assertNotBlank(str, script, developMsg, true);
    }

    public static void assertNotBlank(String str, String script, String developMsg, boolean isForm) {
        if (isForm) {
            assertRestFormLegal(StringUtils.isNotBlank(str), script, developMsg);
        } else {
            assertContextParaLegal(StringUtils.isNotBlank(str), script, developMsg);
        }
    }


}
